/**
 * 
 */
package indexer;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author Pascal de Vink
 * @version 0.2
 */
public class Search extends Thread {

	private File path;
	private Db db;
	private Tag tag;
	private Logger logg;
	
	public Search(File path)
	{
		this.path = path;
		this.db = new Db();
		this.tag = new Tag();
	}
	
	public void run()                       
     {              
		try {
			// Empty the database
			if (Dispatcher.LOG.equals("true"))
			{
				this.logg = new Logger(new File(Dispatcher.LOGFILE));
				this.logg.writeLine("Emptying current database");
			}
			this.db.emptyFile();
			this.db.emptyFolder();
			this.db.emptyTrack();
			this.db.emptyArtist();
			this.db.emptyAlbum();
			
			// Create first parent
			if (Dispatcher.LOG.equals("true"))
			{
				this.logg.writeLine("Settings loaded, indexing started");
			}
			this.db.commitFolder(this.path.getName(), 0);
			
			// Get the index
			this.getIndex(this.path, 1);
			
			if (Dispatcher.LOG.equals("true"))
			{
				this.logg.writeLine("Indexing done, closing database");
			}
			this.db.closeDB();
			if (Dispatcher.LOG.equals("true"))
			{
				this.logg.writeLine("Exiting");
				this.logg.closeLogger();
			}
		} catch (IOException e) {
			this.db.closeDB();
			this.logg.closeLogger();
			e.printStackTrace();
		} catch (SQLException e) {
			this.db.closeDB();
			this.logg.closeLogger();
			e.printStackTrace();
		}
     }
	 
	private void getIndex(File path, int parent) throws IOException, SQLException
	{
		if (path == null || !path.exists()) {
			System.out.println("The source folder is not correct: it is either empty, doesn't exist or the access rights are not properly configured.");
			System.exit(0);
		}
		
		for (File aFile: path.listFiles()) {
			if (aFile.canRead())
			{
				if (aFile.isDirectory()) {
					if (Dispatcher.LOG.equals("true"))
					{
						this.logg.writeLine("folder: "+aFile.getName());
					}
					
					this.db.commitFolder(aFile.getName(), parent);
					this.getIndex(aFile, this.db.searchLastFolder(aFile.getName()));
				} else {
					if (Dispatcher.LOG.equals("true"))
					{
						this.logg.writeLine("file: "+aFile.getName());
					}
					
					if (aFile.getName().length() > 3)
					{
						if (aFile.getName().substring(aFile.getName().length() - 3, aFile.getName().length()).equals("mp3"))
						{
							String artist = this.tag.getArtist(aFile);
							String track = this.tag.getTrack(aFile);
							String album = this.tag.getAlbum(aFile);
							
							this.db.commitFile(aFile.getName(), aFile.length(), parent);
							this.db.commitArtist(artist);
							this.db.commitAlbum(album, this.db.getArtistID(artist), this.db.searchLastFolder(aFile.getParentFile().getName()));
							this.db.commitTrack(track, this.db.getArtistID(artist), this.db.getAlbumID(album), this.db.getFileID(aFile.getName()));
						}
					}
				}
			}
		}
	}
}
